package tinybase.sm;

import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Arrays;

import org.junit.Assert;

import tinybase.basic.RedBase;

public class SM_AttrcatRec {
	public String relName;
	public String attrName;
	public int offset;
	public RedBase.AttrType attrType;
	public int attrLength;
	public int indexNo;
//	public int distinctVals;
//	public int ixDepth;
//	public int minValue;
//	public int maxValue;

	public void storeAsBytes(ByteBuffer buf) {
		buf.clear();
		buf.put(relName.getBytes(Charset.forName("ASCII")), 0, Math.min(relName.length(), RedBase.MAXNAME));
		buf.position(RedBase.MAXNAME);
		buf.put(attrName.getBytes(Charset.forName("ASCII")), 0, Math.min(attrName.length(), RedBase.MAXNAME));
		buf.putInt(getOffsetOffset(), offset);
		buf.putInt(getAttrTypeOffset(), attrType.getValue());
		buf.putInt(getAttrLengthOffset(), attrLength);
		buf.putInt(getIndexNoOffset(), indexNo);
//		buf.putInt(getDistinctValsOffset(), distinctVals);
//		buf.putInt(getIxDepthOffset(), ixDepth);
//		buf.putInt(getMinValueOffset(), minValue);
//		buf.putInt(getMaxValueOffset(), maxValue);
	}
	
	public SM_AttrcatRec(String _relName, String _attrName, int _offset,
			RedBase.AttrType _attrType, int _attrLength, int _indexNo) {
		relName = new String(_relName);
		attrName = new String(_attrName);
		offset = _offset;
		attrType = _attrType;
		attrLength = _attrLength;
		indexNo = _indexNo;
	}
	
	public SM_AttrcatRec(byte[] arr) {
		ByteBuffer buf= ByteBuffer.wrap(arr);
		byte[] temp= new byte[RedBase.MAXNAME];
		buf.get(temp, 0, RedBase.MAXNAME);
		relName= temp.toString();
		buf.position(RedBase.MAXNAME);
		buf.get(temp, 0, RedBase.MAXNAME);
		attrName= temp.toString();
		offset= buf.getInt(getOffsetOffset());
		attrType.setValue(buf.getInt(getAttrTypeOffset()));
		attrLength= buf.getInt(getAttrLengthOffset());
		indexNo= buf.getInt(getIndexNoOffset());
//		distinctVals= buf.getInt(getDistinctValsOffset());
//		ixDepth= buf.getInt(getIxDepthOffset());
//		minValue= buf.getInt(getMinValueOffset());
//		maxValue= buf.getInt(getMaxValueOffset());
	}

	public SM_AttrcatRec() {
		relName = null;
		attrName = null;
		offset = 0;
		attrType = null;
		attrLength = 0;
		indexNo = 0;
	}
	
	public static void SM_SetAttrcatRec(SM_AttrcatRec r, String _relName, String _attrName, int _offset,
			RedBase.AttrType _attrType, int _attrLength, int _indexNo) {
		r.relName = new String(_relName);
		r.attrName = new String(_attrName);
		r.offset = _offset;
		r.attrType = _attrType;
		r.attrLength = _attrLength;
		r.indexNo = _indexNo;
	}
	
	public static void SM_SetAttrcatRec(DataAttrInfo r, String _relName, String _attrName, int _offset,
			RedBase.AttrType _attrType, int _attrLength, int _indexNo) {
		r.relName = new String(_relName);
		r.attrName = new String(_attrName);
		r.offset = _offset;
		r.attrType = _attrType;
		r.attrLength = _attrLength;
		r.indexNo = _indexNo;
	}

	public static int getRelNameOffset() {
		return 0;
	}

	public static int getAttrNameOffset() {
		return RedBase.MAXNAME;
	}

	public static int getOffsetOffset() {
		return RedBase.MAXNAME + RedBase.MAXNAME;
	}

	public static int getAttrTypeOffset() {
		return RedBase.MAXNAME + RedBase.MAXNAME + 4;
	}

	public static int getAttrLengthOffset() {
		return RedBase.MAXNAME + RedBase.MAXNAME + 4 + 4;
	}

	public static int getIndexNoOffset() {
		return RedBase.MAXNAME + RedBase.MAXNAME + 4 + 4 + 4;
	}

	public static int getSM_AttrcatRecSize() {
		return RedBase.MAXNAME + RedBase.MAXNAME + 4 + 4 + 4 + 4;
	}
	
//	public static int getDistinctValsOffset() {
//		return RedBase.MAXNAME + RedBase.MAXNAME + 4 + 4 + 4 + 4;
//	}
//	
//	public static int getIxDepthOffset() {
//		return RedBase.MAXNAME + RedBase.MAXNAME + 4 + 4 + 4 + 4 + 4;
//	}
//	
//	public static int getMinValueOffset() {
//		return RedBase.MAXNAME + RedBase.MAXNAME + 4 + 4 + 4 + 4 + 4 + 4;
//	}
//	
//	public static int getMaxValueOffset() {
//		return RedBase.MAXNAME + RedBase.MAXNAME + 4 + 4 + 4 + 4 + 4 + 4 + 4;
//	}
	

	
	
	
	/**
	 * store the relName member in the container
	 * 
	 * @param container
	 *            : out, the container of result
	 * @param start
	 *            : start index in the container
	 * @return the real length of the name
	 */
	public int getRelNameToByteArray(byte[] container, int start) {
		Assert.assertTrue((container.length - start) > RedBase.MAXNAME);
		Arrays.fill(container, start, start + RedBase.MAXNAME, (byte) 0);
		int length = Math.min(relName.length(), RedBase.MAXNAME);
		System.arraycopy(relName.getBytes(Charset.forName("ASCII")), 0,
				container, start, length);
		return length;
	}

	/**
	 * store the attrName member in the container
	 * 
	 * @param container
	 *            : out, the container of result
	 * @param start
	 *            : start index in the container
	 * @return the real length of the name
	 */
	public int getAttrNameToByteArray(byte[] container, int start) {
		Assert.assertTrue((container.length - start) > RedBase.MAXNAME);
		Arrays.fill(container, start, start + RedBase.MAXNAME, (byte) 0);
		int length = Math.min(attrName.length(), RedBase.MAXNAME);
		System.arraycopy(attrName.getBytes(Charset.forName("ASCII")), 0,
				container, start, length);
		return length;
	}

}
